﻿using System;
using System.Reflection;


public class EnemyAI
{

    private string linkScript;
    private float aiCheckTime;
    private EnemyActiveObject obj;

    private Type linkScriptType;
    private object linkScriptObj;

    public EnemyAI(string script,EnemyActiveObject obj)
    {
        linkScript = script;
        this.obj = obj;

        if (linkScript != null)
        {
            linkScriptType = Type.GetType("Unity.Game.ElisAdvance.MonsterAIScript." + linkScript);
            if (linkScriptType != null)
            {
                object[] constructParms = new object[] { this };  //构造器参数
                linkScriptObj = Activator.CreateInstance(linkScriptType, constructParms);
            }


        }
        aiCheckTime = this.obj.getObjectTime();
    }

    public bool isRest()
    {
        if (obj.getState() == State.STATE_STAND || obj.getState() == State.STATE_MOVE)
            return true;
        return false;
    }

    public bool isJump()
    {
        if (obj.getState() == State.STATE_JUMP)
            return true;
        return false;
    }

    public bool isTargetInArea(float startX,float startY,float offsetX,float offsetY)
    {
        EnemyActiveObject enemyObj = (EnemyActiveObject)obj;
        ActiveObject target = enemyObj.getTarget();
        if (target)
        {
            float startPosX = Com.getDistancePos(obj.getXPos(), obj.getDirection(), startX);
            float endPosX = Com.getDistancePos(startPosX, obj.getDirection(), offsetX);

            float startPosY = obj.getYPos() + startY;
            float endPosY = startPosY + offsetY;

            if (Math2.isInRange(startPosX, endPosX, target.getXPos()) && Math2.isInRange(startPosY,endPosY,target.getYPos()))
            {
                return true;
            }
        }
        return false;
    }


    public void AICheck()
    {
        if (linkScriptType == null)
            return;

        MethodInfo method = linkScriptType.GetMethod("thinkTerm");
        if (method != null)
        {

            float thinkTerm = (float)method.Invoke(linkScriptObj, null);

            if (obj.getObjectTime() - aiCheckTime >= thinkTerm)
            {

                aiCheckTime = obj.getObjectTime();
                MethodInfo methodAction = linkScriptType.GetMethod("action");

                if (methodAction != null)
                {

                    float attack = (float)methodAction.Invoke(linkScriptObj, null);
                    if (attack >= 0.0f)
                    {

                        obj.clearStateVector();
                        obj.pushStateVector(attack);
                        obj.setState(State.STATE_ATTACK);
                        return;   
                    }
                    else
                    {
                        MethodInfo methodMoveEvent = linkScriptType.GetMethod("moveEvent");
                        if (methodMoveEvent != null)
                        {
                            float moveID = (float)methodMoveEvent.Invoke(linkScriptObj, null);

                            if (moveID == -1)
                            {
                                obj.sendStay();
                                return;
                            }else if (moveID == 0)
                            {
                                AIMoveEventTarget.event0(obj);
                                return;
                            }

                        }

                        

                    }
                }
            }
            
        }
        

    }


}

class AIMoveEventTarget
{

    public static void event0(EnemyActiveObject obj)
    {
        ActiveObject target = obj.getTarget();
        if (target)
        {
            if (target.getXPos() > obj.getXPos())
            {
                obj.sendXMove(Direction.RIGHT);
            }
            else if (target.getXPos() < obj.getXPos())
            {
                obj.sendXMove(Direction.LEFT);
            }
            //obj.sendXMoveLeft();
        }
        //obj.DebugString("print:event0");
    }

}